package com.group.nepm.statisticDataManagement.controller;

import com.group.common.utils.MyUtils;
import com.group.nepg.entity.AccurateAqiLevel;
import com.group.nepm.statisticDataManagement.entity.StatisticData;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;


public class AqiTendencyStatisticController {
    @FXML
    private Label January;
    @FXML
    private Label February;
    @FXML
    private Label March;
    @FXML
    private Label April;
    @FXML
    private Label May;
    @FXML
    private Label June;
    @FXML
    private Label July;
    @FXML
    private Label August;
    @FXML
    private Label September;
    @FXML
    private Label October;
    @FXML
    private Label November;
    @FXML
    private Label December;

    public void adminOut(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/common/view/NEPMLoginView.fxml");
    }
    public void toPublicSuperviseDataList(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/publicSuperviseDataManagement/view/PublicSuperviseDataList.fxml");
    }
    public void toConfirmAqiDataList(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/publicSuperviseDataManagement/view/ConfirmAqiDataList.fxml");
    }
    public void toProvinceStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/provinceStatistic.fxml");
    }
    public void toAqiDistributionStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/aqiDistributionStatistic.fxml");
    }
    public void toAqiTendencyStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/aqiTendencyStatistic.fxml");
    }
    public void toOtherDataStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/otherDataStatistic.fxml");
    }

    public void initialize() {
        //读取数据
        String filePath = "group/data/statistic_data_arraylist.dat";
        ArrayList<StatisticData> dataList = MyUtils.readFileToArrayList(filePath);

        if (dataList == null || dataList.isEmpty()) {
            System.out.println("未找到数据或数据为空");
            return;
        }
        //遍历数据，统计每个月的AQI等级为4以上的数量
        Map<Integer, Integer> monthCountMap = new HashMap<>();
        for (StatisticData data : dataList) {
            AccurateAqiLevel level = data.getAccurateAqiData().getAccurateAqiLevel();
            if (level != null && level.ordinal() > 3) {
                LocalDate date = data.getRowAqiData().getTime();
                int monthValue = date.getMonthValue();
                monthCountMap.put(monthValue, monthCountMap.getOrDefault(monthValue, 0) + 1);
            }
        }

        setLabelText(January,   monthCountMap.get(1));
        setLabelText(February,  monthCountMap.get(2));
        setLabelText(March,     monthCountMap.get(3));
        setLabelText(April,     monthCountMap.get(4));
        setLabelText(May,       monthCountMap.get(5));
        setLabelText(June,      monthCountMap.get(6));
        setLabelText(July,      monthCountMap.get(7));
        setLabelText(August,    monthCountMap.get(8));
        setLabelText(September, monthCountMap.get(9));
        setLabelText(October,   monthCountMap.get(10));
        setLabelText(November,  monthCountMap.get(11));
        setLabelText(December,  monthCountMap.get(12));
    }

    private void setLabelText(Label label, Integer count) {
        label.setText(count == null ? "0" : count.toString());
    }
}
